import { config } from '../../utils/config'
import { HTTP } from '../../utils/http.js'
import api from '../../utils/api.js'
let http = new HTTP()
// https://images.weigeee.com/20200723/c81f578c724b46a97e41f592a0841c54.png 可领取图
// https://images.weigeee.com/20200723/eb81ede921b0783754d618e86a834c25.png 已领取
Page({
    data: {
        imgUrl: config.imgUrl,
        username: '',
        userImg: '',
        myFriendNum: 1,
        phone: '',
        userShopname: '',
        eggNum: 0
    },

    toMyFriend() {
        wx.navigateTo({
            url: '/pages/myfriend/myfriend?tab=' + 1,
        })
    },
    toJiFenShop() {
        wx.navigateTo({
            url: '/pages/pointsMall/index/index',
        })
    },
    toGeXianMi() {  //葛仙米介绍
        wx.navigateTo({
            url: '/pages/shopGeXianMi/shopGeXianMi',
        })
    },
    toTuiGuang() {
        wx.navigateTo({
            url: '/pages/vippromotion/vippromotion',
        })
    },
    shopPhone(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    shopName(e) {
        this.setData({
            userShopname: e.detail.value
        })
    },
    idMenberGexianMi() {
        http.request({
            url: api.menberGexianMi,
            header: {
                'token': wx.getStorageSync("token")
            },
            method: 'get',
            success: res => {
                if (res.code == 200) {
                    this.setData({
                        gexianmirecord: res.data.gexianmirecord,
                        eggNum: res.data.useryiling,
                        angel: res.data.speedprogress
                    })
                }
            }
        })
    },
    onSubmit() {
        let username = this.data.userShopname
        let phone = this.data.phone
        if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone)) {
            wx.showToast({
                title: '请正确填写电话',
                icon: 'none'
            })
        } else {
            if (username && phone) {
                http.request({
                    url: api.menberFrom,
                    method: 'post',
                    header: {
                        'token': wx.getStorageSync("token")
                    },
                    data: {
                        shop_phone: phone,
                        shop_name: username
                    },
                    success: res => {
                        console.log(res)
                        if (res.code == 200) {
                            wx.showModal({
                                content: '稍后会有客服人员联系您，\r\n请保持电话通畅。',
                                showCancel: false,
                                confirmColor: '#EA5D56',
                                success: function (e) {
                                    if (e.confirm) {
                                        wx.navigateBack({
                                            complete: (res) => { },
                                        })
                                    }
                                }
                            })
                        } else {
                            console.log(res.message)
                            this.setData({
                                error: res.message
                            })
                        }
                    }
                })

            } else {
                wx.showToast({
                    title: '请输入完整信息',
                    icon: 'none',
                    mask: true
                })
            }
        }
    },
    onLoad: function () {
        this.idMenberGexianMi()
    },
    //之前内容
    onShow: function () {
        this.setData({
            username: wx.getStorageSync('nickName'),
            userImg: wx.getStorageSync('avatarUrl')
        })
        this.reqCode()
        this.getNum()
    },
    //获取好友数量
    getNum() {
        var that = this
        http.request({
            url: api.getFriendNum,
            header: {
                token: wx.getStorageSync('token')
            },
            method: 'get',
            success: function (res) {
                if (res.code == 200) {
                    that.setData({
                        myFriendNum: res.data.num
                    })
                }
            }
        })
    },
    //获取用户邀请码
    reqCode() {
        http.request({
            url: '/client/getInviteCode',
            header: {
                'token': wx.getStorageSync("token")
            },
            success: res => {
                console.log(res.data.invite_code)
                try {
                    wx.setStorageSync('invcode', res.data.invite_code)
                } catch (e) { }
            }
        })
    },

    onShareAppMessage: function (res) {
        console.log(wx.getStorageSync('invcode'))
        let url = '/pages/vippromotion/vippromotion?code=' + wx.getStorageSync('invcode');
        return {
            title: '成为味歌会员获取六大会员权益',
            path: '/pages/index/index?GoTo=' + encodeURIComponent(url),
            // imageUrl: config.imgUrl + '/images/share_card_img.png',
            imageUrl: config.imgUrl + '/images/hh5rt78i9098.png',
            // imageUrl: '../../images/share.png',
            success: function (res) {
                // 转发成功
                wx.showToast({
                    title: "分享成功",
                    icon: 'success',
                    duration: 1500
                })
            },
            fail: function (res) {
                // 转发失败
                wx.showToast({
                    title: "分享失败",
                    duration: 2001
                })
            }
        }
    },


})